package at.fhj.ase.clientarch.server.db.hibernate.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import at.fhj.ase.clientarch.common.util.StringUtil;

/**
 * @author cja, sfe | www.fh-joanneum.at | Client Architectures and Design
 */
@Entity
@Table(name = EntityUser.TABLE_NAME)
public class EntityUser {
  static final String TABLE_NAME = "USER";
  private String username;
  private String password;
  private long id;

  /** We HAVE to specify a default constructor for hibernate */
  @SuppressWarnings("unused")
  private EntityUser() {
  }

  public EntityUser(final String username, final String password) {
    setUsername(username);
    setPassword(password);
  }

  /* Getter and Setter */

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public long getId() {
    return id;
  }

  @SuppressWarnings("unused")
  private void setId(long id) {
    this.id = id;
  }

  @Column(nullable = false, unique = true)
  public String getUsername() {
    return username;
  }

  private void setUsername(String username) {
    StringUtil.checkEmptyOrNull(username);
    this.username = username;
  }

  @Column(nullable = false)
  public String getPassword() {
    return password;
  }

  private void setPassword(String password) {
    StringUtil.checkEmptyOrNull(password);
    this.password = password;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((password == null) ? 0 : password.hashCode());
    result = prime * result + ((username == null) ? 0 : username.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    final EntityUser other = (EntityUser) obj;
    if (password.equals(other.password) && username.equals(other.username))
      return true;
    return true;
  }

  public String toString() {
    return getClass().getSimpleName() + ": ID: " + id + ", " + username + ", " + password;
  }
}
